package com.freshcity.page.wait
{
	import com.freshcity.component.container.Component;
	import com.freshcity.core.Dispatcher;
	import com.freshcity.core.MainScene;
	import com.freshcity.core.OpCodes;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.utils.StringUtil;
	import com.freshcity.wait.ComeInMsgWin;
	import com.greensock.TimelineMax;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.pblabs.debug.Logger;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;

	public class ComeInMsg extends Component
	{
		private var _msgs:Vector.<String>;
		private var back:ComeInMsgWin;
		private var _timeLine:TimelineMax;
		private var isFinish:Boolean;
		private var isStart:Boolean;
		private var currUser:String;

		public function ComeInMsg(main:MainScene)
		{
			this.main=main;
			this.visible=false;
			_msgs=new Vector.<String>();
			_timeLine=new TimelineMax({repeat: -1});
			_timeLine.addCallback(onTick, 0.1);			
			
			super(LayerManager.msgTipLayer, 0, 0);
			this.setSize(200, 100);
			back.closeBtn.addEventListener(MouseEvent.CLICK,hide);
		}

		override protected function addChildren():void
		{
			super.addChildren();
			back=new ComeInMsgWin();
			addChild(back);
			
			var loc1:TextField=StringUtil.addHyLink("好啊", "", onWatchUser);
			loc1.x=160;
			loc1.y=75;
			addChild(loc1);
			
			var loc2:TextField=StringUtil.addHyLink("没兴趣", "", hide);
			loc2.x=110;
			loc2.y=75;
			addChild(loc2);
		}

		override public function draw():void
		{
			super.draw();
			reset();
		}
		
		public function onTick():void
		{
			//Logger.debug(this,"onTick",String(this._msgs.length));
			if (this._msgs.length > 0 && this.isFinish)
			{
				this.currUser=this._msgs.shift();
				this.isFinish=false;
				main.msg.showFixRollMsg("用户 "+this.currUser+" 来了");
				back.words.text="".concat("用户", currUser, "刚刚进来，要不要和TA打个招呼呢？");
				show();
				if(this._msgs.length > 0){
					TweenLite.delayedCall(1,hide);
				}
			}
			else if(this._msgs.length <= 0)
			{
				_timeLine.stop();
				this.isStart=false;
			}
		}		

		private function onWatchUser(evt:TextEvent):void
		{
			var u:String = this.currUser;
			Logger.debug(this,"the current user is ",u);
			this.hide();
			Dispatcher.addEventListener(EventName.UserReadDone,main.showUser);
			main.findUser(u);
		}

		public function set data(data:*):void
		{
			_msgs.push(data.user);
			
			if (!isStart)
			{
				_timeLine.play();
				this.isStart=true;
			}
		}
		
		private function reset():void
		{
			this.x=(main.stage.stageWidth - this.width) - 3;
			this.y = (main.stage.stageHeight -this.height) + 48;
			this.isFinish = true;
		}

		override public function show(event:Event=null):void
		{
			TweenMax.to(this, 0.5, {autoAlpha: 1, y: "-50"});
		}
		
		override public function hide(event:Event=null):void
		{
			TweenMax.to(this, 0.2, {autoAlpha: 0, y: "+50",onComplete:reset});
		}
		
		override public function destroy():void
		{
			back.closeBtn.removeEventListener(MouseEvent.CLICK,hide);
			//EffectManager.hideUIMask();
			LayerManager.msgTipLayer.removeChild(this);
		}

	}
}
